package com.cskaoyan.service.wx.lghc;

import com.cskaoyan.controller.wx.wxy.OrderUtils;
import com.cskaoyan.mapper.MarketAftersaleMapper;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.module.bean.vo.AfterSaleConnectGoodOrderVO;
import com.cskaoyan.module.bean.vo.MallAftersaleVO;
import com.cskaoyan.module.bean.vo.VxAfterSaleVO;
import com.cskaoyan.module.pojo.*;
import com.cskaoyan.module.vo.wx.wxy.AftersaleDetailDataVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author :ligenghuisheng
 * @Description:
 * @Date :2022/4/8  16:44
 */
@Service
public class VxAfterSaleServiceImpl implements VxAfterSaleService {

    @Autowired
    MarketAftersaleMapper aftersaleMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Autowired
    MarketOrderMapper orderMapper;


    @Override
    public VxAfterSaleVO listAfterSale(VxAfterSaleVO vxAfterSaleVO, MarketAftersale marketAftersale, MarketOrderGoods marketOrderGoods) {

        PageHelper.startPage(vxAfterSaleVO.getPage(),vxAfterSaleVO.getLimit());
        //生成example对象forAfterSlae
        MarketAftersaleExample example = new MarketAftersaleExample();
        //生成example对象forOrderGoods
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        //生成List类下面的对象

        //使用这个createCriteria方法创建Example的内部类对象 → 里面维护了条件列表
        MarketAftersaleExample.Criteria criteria = example.createCriteria();
        //生成List对象
        List<AfterSaleConnectGoodOrderVO> marketOrderGoods4 = new LinkedList<>();


        criteria.andStatusEqualTo(marketAftersale.getStatus());
        List<MarketAftersale>   marketAftersales1 =   aftersaleMapper.selectByExample(example);

        List<MarketOrderGoods> marketOrderGoods2 = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
        PageInfo<MarketAftersale> pageInfo = new PageInfo<>(marketAftersales1);

                for (MarketAftersale aftersale : marketAftersales1) {
                    AfterSaleConnectGoodOrderVO afterSaleConnectGoodOrderVO = new AfterSaleConnectGoodOrderVO();
                    afterSaleConnectGoodOrderVO.setAftersale(aftersale);
                    afterSaleConnectGoodOrderVO.setGoodsList(marketOrderGoods2);
                    marketOrderGoods4.add(afterSaleConnectGoodOrderVO);
                    vxAfterSaleVO.setList(marketOrderGoods4);
        }







        long total = pageInfo.getTotal();
        int pageSize = pageInfo.getPageSize();

        vxAfterSaleVO.setLimit(vxAfterSaleVO.getLimit());
        vxAfterSaleVO.setPage(vxAfterSaleVO.getPage());
        vxAfterSaleVO.setTotal((int) total);
        vxAfterSaleVO.setPages(vxAfterSaleVO.getPage());


        return vxAfterSaleVO;
    }

    @Override
    public AftersaleDetailDataVo selectAftersaleOrOrder(Integer orderId) {
        MarketOrder marketOrder = orderMapper.selectByPrimaryKey(orderId);

        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(orderGoodsExample);

        MarketAftersale marketAftersale = new MarketAftersale();
        marketAftersale.setOrderId(orderId);
        MarketAftersale marketAftersale1 = aftersaleMapper.selectByAfterSale(marketAftersale);

        return AftersaleDetailDataVo.data(marketAftersale1,marketOrder,marketOrderGoods);
    }

    @Override
    public int updateAftersale(Integer orderId, BigDecimal amount, String[] pictures, String reason, Short type, String typeDesc) {
        MarketAftersale marketAftersale = new MarketAftersale();
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser)subject.getPrincipals().getPrimaryPrincipal();


        marketAftersale.setId(null);
        marketAftersale.setAftersaleSn(OrderUtils.getRefundCode(user.getId()));
        marketAftersale.setOrderId(orderId);
        marketAftersale.setUserId(user.getId());
        marketAftersale.setType(type);
        marketAftersale.setReason(reason);
        marketAftersale.setAmount(amount);
        marketAftersale.setPictures(pictures);
        marketAftersale.setStatus((short) 1);

        Date date = null;
        try {
            date = new SimpleDateFormat().parse("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketAftersale.setAddTime(date);
        marketAftersale.setUpdateTime(date);


        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setRefundAmount(amount);
        marketOrder.setRefundType(type.toString());
        marketOrder.setRefundTime(date);
        marketOrder.setUpdateTime(date);
        orderMapper.updateByPrimaryKeySelective(marketOrder);

        int code = aftersaleMapper.insertSelective(marketAftersale);

        return code;
    }

}
